<template>
  <div class="main-wrapper">
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="名称" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.status" placeholder="状态" clearable class="filter-item">
        <el-option v-for="(value, key) in status" :key="key" :label="value" :value="key" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row>
      <el-table-column label="项目名称" align="center">
        <template slot-scope="{ row }">
          {{ row.name }}
        </template>
      </el-table-column>
      <el-table-column label="所在省市" align="center">
        <template slot-scope="{ row }">
          {{ row.provinceName }}{{ row.cityName }}
        </template>
      </el-table-column>
      <el-table-column label="项目价格" align="center">
        <template slot-scope="{ row }">
          {{ row.money }}
        </template>
      </el-table-column>
      <el-table-column label="项目类型" align="center">
        <template slot-scope="{ row }">
          {{ row.categoryName }}
        </template>
      </el-table-column>
      <el-table-column label="专业" align="center">
        <template slot-scope="{ row }">
          {{ row.professionName }}
        </template>
      </el-table-column>
      <el-table-column label="提交时间" width="100" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.createTime | timeFilter('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="98">
        <template slot-scope="{ row }">
          <el-tag :type="row.status | styleFilter()">{{
          statusData[row.status].title
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="提交人" align="center">
        <template slot-scope="{ row }">
          {{ row.userName }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" max-width="160">
        <template slot-scope="{ row }">
          <el-button class="row-btn" type="primary" size="mini" @click="openDetail(row)">
            详情
          </el-button>
          <el-button v-if="row.status === 0" class="row-btn" type="warning" size="mini" @click="openVerifyForm(row)">
            确认
          </el-button>
          <el-button v-if="row.status === 1" class="row-btn" type="primary" size="mini" @click="handleSettle(row)">
            结算
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize"
      @pagination="fetchProjectList" />

    <el-dialog title="项目详情" width="750px" :visible.sync="dialogFormVisible">
      <div v-show="dialogFormVisible" class="detail">
        <el-descriptions v-if="selectedProject" :column="3" border size="Large" :label-style="{ color: '#909399' }">
          <el-descriptions-item label="项目名称" span="2">{{
          getDisplayString(selectedProject.name)
          }}</el-descriptions-item>
          <el-descriptions-item label="项目金额">{{
          selectedProject.money | amountFilter()
          }}</el-descriptions-item>
          <el-descriptions-item label="所在省市">
            {{ getDisplayString(selectedProject.provinceName) }}
            {{ getDisplayString(selectedProject.cityName) }}
          </el-descriptions-item>
          <el-descriptions-item label="项目类型">{{
          getDisplayString(selectedProject.categoryName)
          }}</el-descriptions-item>
          <el-descriptions-item label="项目专业">{{
          getDisplayString(selectedProject.professionName)
          }}</el-descriptions-item>
          <el-descriptions-item label="客户单位">{{
          getDisplayString(selectedProject.orgName)
          }}</el-descriptions-item>
          <el-descriptions-item label="客户联系人" span="2">{{
          getDisplayString(selectedProject.contactPerson)
          }}</el-descriptions-item>
          <el-descriptions-item label="联系方式-座机">{{
          getDisplayString(selectedProject.phoneNo)
          }}</el-descriptions-item>
          <el-descriptions-item label="联系方式-手机" span="2">{{
          getDisplayString(selectedProject.mobile)
          }}</el-descriptions-item>
          <el-descriptions-item label="备注信息" span="3">
            {{ getDisplayString(selectedProject.remark) }}
          </el-descriptions-item>
          <el-descriptions-item label="提交时间" span="1">
            {{ selectedProject.createTime | timeFilter('{y}-{m}-{d} {h}:{i}') }}
          </el-descriptions-item>
          <el-descriptions-item label="提交人" span="1">
            {{ selectedProject.userName }}
          </el-descriptions-item>
        </el-descriptions>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleDialogClose()">关闭</el-button>
      </div>
    </el-dialog>

    <el-dialog title="项目确认" width="400px" :visible.sync="dialogEditVisible">
      <template #title>
        {{ verifyForm.name }}
      </template>
      <el-form ref="verifyForm" :model="verifyForm" label-position="right" label-width="100px">
        <el-form-item label="确认项目金额">
          <el-input v-model="verifyForm.money" style="width:80%;">
            <template #suffix>元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="操作" prop="status">
          <el-radio-group v-model="verifyForm.status">
            <el-radio :label="1">确认项目</el-radio>
            <el-radio :label="2" style="color:red;">取消项目</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditVisible = false">取消</el-button>
        <el-button type="primary" @click="handleVerify()">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAgentProjectList,
  verifyAgentProject,
  settleAgentProject
} from '@/api/agentProject'
import { parseTime, smallToBig } from '@/utils'
import Pagination from '@/components/Pagination'

export default {
  name: 'Project',
  components: {
    Pagination
  },
  filters: {
    styleFilter (style) {
      const styleMap = { 0: 'warning', 1: 'success', 2: 'danger' }
      return styleMap[style]
    },
    timeFilter (time, format) {
      return parseTime(time, format)
    },
    amountFilter (value, currency = '元', decimals = 0) {
      const digitsRE = /(\d{3})(?=\d)/g
      value = parseFloat(value)
      if (!value && value !== 0) return ''
      const stringified = Math.abs(value).toFixed(decimals)
      const $int = decimals ? stringified.slice(0, -1 - decimals) : stringified
      const i = $int.length % 3
      const head = i > 0 ? $int.slice(0, i) + ($int.length > 3 ? ',' : '') : ''
      const $float = decimals ? stringified.slice(-1 - decimals) : ''
      const sign = value < 0 ? '-' : ''
      return `${sign}${head}${$int
        .slice(i)
        .replace(digitsRE, '$1,')}${$float} ${currency}`
    }
  },
  data () {
    return {
      statusData: {
        0: {
          title: '待确认'
        },
        1: {
          title: '进行中'
        },
        2: {
          title: '已取消'
        },
        5: {
          title: '已结算'
        }
      },
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        pageSize: 20
      },
      projectData: {},
      selectedProject: null,
      dialogFormVisible: false,
      dialogEditVisible: false,
      status: [],
      verifyForm: {}
    }
  },
  computed: {},
  created () {
    this.fetchProjectList()
  },
  methods: {
    fetchProjectList () {
      this.listLoading = true
      getAgentProjectList({}).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter () {
      this.listQuery.page = 1
      this.fetchProjectList()
    },
    resetProjectData () {
      this.projectData = {}
    },
    openDetail (row) {
      this.selectedProject = row
      this.dialogFormVisible = true
    },
    handleDialogClose () {
      this.dialogFormVisible = false
      this.selectedProject = null
    },
    getDisplayString (str) {
      return str || ' - '
    },
    openVerifyForm (row) {
      this.verifyForm = { ...row }
      this.verifyForm.status = 1
      this.dialogEditVisible = true
    },
    handleVerify () {
      verifyAgentProject(this.verifyForm)
        .then(res => {
          this.$notify({
            title: '提交成功',
            type: 'success',
            duration: 2000
          })
          this.dialogEditVisible = false
          this.fetchProjectList()
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    handleSettle (project) {
      this.$confirm(`<div><p>项目: ${project.name}</p><p>金额: <span style="color: red">￥${project.money}</span></p><p style="margin-left: 38px;color: red">${smallToBig(project.money)}</p></div>`, '请确认是否结算项目', {
        dangerouslyUseHTMLString: true,
        confirmButtonText: '结算',
        cancelButtonText: '取消',
      }).then(() => {
        settleAgentProject(project.id)
          .then(res => {
            this.$notify({
              title: '成功',
              message: '结算信息提交完成',
              type: 'success',
              duration: 2000
            })
            this.fetchProjectList()
          })
          .catch(err => {
            console.log('err :>> ', err)
            this.$notify({
              title: '结算失败',
              message: '结算提交失败,请联系管理员',
              type: 'error',
              duration: 2000
            })
          })
      }).catch(err => {
        this.$message({
          type: 'info',
          message: '取消结算'
        });
      });
    },

  }
}
</script>
<style lang="scss" scoped>
.main-wrapper {
  margin: 8px;

  .filter-container {
    padding: 10px 0;

    .filter-item {
      width: 200px;
      margin-right: 10px;
    }
  }

  .dialog-footer {
    text-align: center;
  }

  .row-btn {
    margin: 2px 2px;
  }
}
</style>
